import { _decorator, Button, Component, Label, Node, Sprite } from 'cc';
import { BasicUI } from '../../Init/Basic/BasicUI';
import { UIExitAnimtions } from '../../Init/Tools/UIExitAnimations';
import { UIEnum } from '../../Init/SystemUI/UIEnum';
import { UISystem } from '../../Init/SystemUI/UISystem';
import { StorageSystem } from '../../Init/SystemStorage/StorageSystem';
import Tools from '../../Init/Tools/Tools';
import EventManager from '../../Init/Managers/EventManager';
import { EventTypes } from '../../Init/Managers/EventTypes';
import { GlobalEnum } from '../../Init/Config/GlobalEnum';
const { ccclass, property } = _decorator;

enum AwardType {
    Gold = 1,
    Energy,
    Chip,
    prop1,
    prop2,
    prop3,
}

const SignData = {
    1: [{ type: AwardType.Gold, award: 200 }],
    2: [{ type: AwardType.Energy, award: 50 }],
    3: [{ type: AwardType.Chip, award: 15 }],
    4: [{ type: AwardType.prop1, award: 5 }],
    5: [{ type: AwardType.prop2, award: 3 }],
    6: [{ type: AwardType.prop3, award: 3 }],
    7: [{ type: AwardType.Energy, award: 50 }, { type: AwardType.Chip, award: 10 }],
}

@ccclass('SignUI')
export class SignUI extends BasicUI {

    @property(Node)
    bg: Node = null;
    @property(Node)
    panel: Node = null;

    protected onLoad(): void {
        super.onLoad();
        this.node.on(Node.EventType.TOUCH_START, this.onCloseClick, this);
    }

    public show(d?: any): void {
        super.show(d);
        this.initSignInfo();
        this.refreshSignInfo();
    }

    public hide(d?: any): void {
        super.hide(d);

    }
    hideAnim() {
        this.bg.getComponent(UIExitAnimtions).showHideAnim(false);
        this.panel.getComponent(UIExitAnimtions).showHideAnim(false, () => {
            UISystem.hideUI(UIEnum.SignUI);
        }, true);
    }

    //#region ---按钮
    onCloseClick() {
        this.hideAnim();
    }
    onSignClick() {
        this.awardSign();
    }
    //#endregion

    //#region ---签到逻辑
    @property(Node)
    signBtn: Node = null;

    @property(Node)
    layout: Node = null;
    @property(Node)
    item7: Node = null;

    isInit = false;

    initSignInfo() {
        if (this.isInit) return;
        // 设置奖励显示
        // 1-6
        for (let i = 0; i < this.layout.children.length; i++) {
            const e = this.layout.children[i];
            const awardLabel = e.getChildByName('award').getComponent(Label);
            awardLabel.string = 'x' + SignData[i + 1][0].award.toFixed(0);
        }
        // 7
        const award1 = this.item7.getChildByName('award1');
        const award2 = this.item7.getChildByName('award2');
        award1.getComponent(Label).string = 'x' + SignData[7][0].award.toFixed(0);
        award2.getComponent(Label).string = 'x' + SignData[7][1].award.toFixed(0);
    }

    refreshSignInfo() {
        const userAssets = StorageSystem.getData().userAssets;
        const signData = userAssets.signData;
        // 判断当天是否签到
        const isChecked = Tools.isSameDay(signData.lastTime);

        if (!isChecked && signData.dayIdx >= 7) {
            // 第8天奖励重置 循环签到
            signData.dayIdx = 0;
        }

        this.setSignBtn(isChecked);
        // 判断领取状态
        for (let i = 0; i < this.layout.children.length; i++) {
            const e = this.layout.children[i];
            e.getChildByName('checked').active = i < signData.dayIdx;
        }
        this.item7.getChildByName('checked').active = isChecked && signData.dayIdx == 7;
    }

    setSignBtn(isChecked) {
        this.signBtn.getComponent(Sprite).grayscale = isChecked;
        this.signBtn.getComponent(Button).interactable = !isChecked;
    }

    awardSign() {
        const userAssets = StorageSystem.getData().userAssets;

        // 记录签到时间
        userAssets.signData.lastTime = new Date().getTime();
        userAssets.signData.dayIdx++;
        userAssets.signData.count++;
        // 获取奖励
        const awardArr: { type: AwardType, award: number }[] = SignData[userAssets.signData.dayIdx];
        let addChipNum = 0;

        for (let i = 0; i < awardArr.length; i++) {
            const award = awardArr[i];
            switch (award.type) {
                case AwardType.Gold:
                    userAssets.asset += award.award;
                    break;
                case AwardType.Energy:
                    userAssets.energy += award.award;
                    break;
                case AwardType.Chip:
                    addChipNum += award.award;
                    break;
                // case AwardType.prop1:
                //     userAssets.propCountRecs[GlobalEnum.GridPropType.prop1] += award.award;
                //     break;
                // case AwardType.prop2:
                //     userAssets.propCountRecs[GlobalEnum.GridPropType.prop2] += award.award;
                //     break;
                // case AwardType.prop3:
                //     userAssets.propCountRecs[GlobalEnum.GridPropType.prop3] += award.award;
                    break;
                default:
                    break;
            }
        }
        StorageSystem.saveData();

        StorageSystem.updateToAssets(true, false);

        this.hideAnim();
        this.refreshSignInfo();
        EventManager.emit(EventTypes.SignUIEvents.SignSuccess);
    }
    //#endregion

}

